package com.jpress.model;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;


@Entity
@Table(name="categoria")
@NamedQueries({
	@NamedQuery(name="Categoria.countCategorias", query="SELECT COUNT(c) FROM Categoria c"),
	@NamedQuery(name="Categoria.findByNome", query="SELECT c FROM Categoria c WHERE c.nome = :nome"),
	@NamedQuery(name="Categoria.findByCodigo", query="SELECT c FROM Categoria c WHERE c.codigo = :codigo"),
	@NamedQuery(name="Categoria.SearchByNome", query="SELECT c FROM Categoria c WHERE c.nome LIKE :nome ORDER BY c.nome ASC"),
	@NamedQuery(name="Categoria.SearchByDescricao", query="SELECT c FROM Categoria c WHERE c.descricao LIKE :descricao"),
	@NamedQuery(name="Categoria.findAll", query="SELECT c FROM Categoria c"),
	@NamedQuery(name="Categoria.findAllDesc", query="SELECT c FROM Categoria c ORDER BY c.id DESC"),
	@NamedQuery(name="Categoria.Search", query="SELECT c FROM Categoria c WHERE c.nome LIKE :nome AND c.descricao LIKE :descricao"),
	@NamedQuery(name="Categoria.ultimasCadastradas", query="SELECT c FROM Categoria c ORDER BY c.codigo DESC")
})
public class Categoria implements Serializable {
	
	private static final long serialVersionUID = 6363231633656994513L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="codigo")
	private int codigo;
	
	@Column(name="nome", length=100, unique=true, nullable=false)
	private String nome;
	
	@Column(name="descricao", length=255, nullable=false)
	private String descricao;
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="categoria")
	private List<Post> posts;
	
	public Categoria(){
		this.posts = new ArrayList<Post>();
	}

	public int getCodigo() {
		return codigo;
	}

	public void setCodigo(int codigo) {
		this.codigo = codigo;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getDescricao() {
		return descricao;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	public List<Post> getPosts() {
		return posts;
	}

	public void setPosts(List<Post> posts) {
		this.posts = posts;
	}

	public void adicionarPost(Post post){
		this.posts.add(post);
	}

	@Override
	public boolean equals(Object object){
		return EqualsBuilder.reflectionEquals(this, object);
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}
}